package com.uplink.msmq.util;

import com.alibaba.fastjson.JSON;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

/**
 * json 相关的帮助类
 *
 * @author oscar
 */
public class JsonUtil {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        // 允许出现单引号
        objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
    }

    /**
     * 将字符串转换成jackson的json对象
     *
     * @param str
     * @return
     * @throws JsonProcessingException
     * @throws IOException
     */
    public static JsonNode getJsonByString(String str) throws JsonProcessingException, IOException {
        if (str == null || "".equals(str)) {
            return null;
        }
        return objectMapper.readTree(str);
    }

    public static String getStringfromMap(Map map) throws JsonGenerationException, JsonMappingException, IOException {
        if (map == null) {
            return null;
        }
        return objectMapper.writeValueAsString(map);
    }

    /**
     * 获得
     *
     * @param o
     * @return
     */
    public static String getJsonStrByObject(Object o) {
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, Object> getFirstLevelKV(String str) {
        if (str == null) {
            return null;
        }
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            JsonNode json = getJsonByString(str);
            if (json != null && json.size() > 0) {
                Iterator<Entry<String, JsonNode>> vNodes = json.getFields();
                //Iterator<JsonNode> vNodes  = json.iterator();
                while (vNodes.hasNext()) {
                    Entry<String, JsonNode> entry = vNodes.next();
                    String key = entry.getKey();
                    JsonNode node = entry.getValue();
                    if (node.isInt()) {
                        result.put(key, node.asInt());
                    }
                    if (node.isLong()) {
                        result.put(key, node.asLong());
                    }
                    if (node.isTextual()) {
                        result.put(key, node.asText());
                    }
                }
                if (result == null || result.isEmpty()) {
                    return null;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    public static List<Map<String, Object>> getListKV(String str) {
        //LogUtils.println(str);
        if (str == null) {
            return null;
        }
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            JsonNode listNode = getJsonByString(str);
            if (listNode != null && listNode.size() > 0) {
                for (int i = 0; i < listNode.size(); i++) {
                    JsonNode json = listNode.get(i);
                    if (json != null && json.size() > 0) {
                        Map<String, Object> result = new HashMap<String, Object>();
                        Iterator<Entry<String, JsonNode>> vNodes = json.getFields();
                        //Iterator<JsonNode> vNodes  = json.iterator();
                        while (vNodes.hasNext()) {
                            Entry<String, JsonNode> entry = vNodes.next();
                            String key = entry.getKey();
                            JsonNode node = entry.getValue();
                            if (node.isInt()) {
                                result.put(key, node.asInt());
                            }
                            if (node.isLong()) {
                                result.put(key, node.asLong());
                            }
                            if (node.isTextual()) {
                                result.put(key, node.asText());
                            }
                        }
                        if (result == null || result.isEmpty()) {
                            continue;
                        } else {
                            list.add(result);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return list;
        }
        return list;
    }

    public static List<String> getMessageList(String data) {
        try {
            JsonNode node = getJsonByString(data);
            if (!node.isArray()) {
                return null;
            } else {
                List<String> result = new ArrayList<String>();
                for (int i = 0; i < node.size(); i++) {
                    result.add(node.get(i).toString());
                }
                return result;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @ Description:  json字符串转换为MAP || 下划线转驼峰
     * @ Author: hz.guo
     * @ Date: 2019-09-23 14:17
     */
    public static Map jsonStrToMaplineToHump(String s) {
        Map map = new HashMap();
        //注意这里JSONObject引入的是net.sf.json
        net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(s);
        Iterator keys = json.keys();
        while (keys.hasNext()) {
            String key = String.valueOf(keys.next());
            String value = json.get(key).toString();
            if (value.startsWith("{") && value.endsWith("}")) {
                map.put(lineToHump.lineToHump(key), jsonStrToMaplineToHump(value));
            } else {
                map.put(lineToHump.lineToHump(key), value);
            }

        }
        return map;
    }

    /**
     * @ Description:  json字符串转换为MAP || 驼峰转下划线
     * @ Author: hz.guo
     * @ Date: 2019-09-23 14:17
     */
    public static Map jsonStrToMapHumpToline(String s) {
        Map map = new HashMap();
        //注意这里JSONObject引入的是net.sf.json
        net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(s);
        Iterator keys = json.keys();
        while (keys.hasNext()) {
            String key = String.valueOf(keys.next());
            String value = json.get(key).toString();
            if (value.startsWith("{") && value.endsWith("}")) {
                map.put(lineToHump.humpToLine(key), jsonStrToMapHumpToline(value));
            } else {
                map.put(lineToHump.humpToLine(key), value);
            }

        }
        return map;
    }

    // 将jsonArray字符串转换成List集合
    public static List<Map> jsonToList(String json) {//PendingInfo可以换成其它bean类
        List<Object> list = JSON.parseArray(json);
        List<Map> listw = new ArrayList<Map>();
        for (Object object : list) {
            Map<String, Object> ageMap = new HashMap<String, Object>();
            Map<String, Object> ret = (Map<String, Object>) object;//取出list里面的值转为map
            listw.add(ret);
        }
        return listw;
    }

    //List集合转换为json
    public static JSON listToJson(List list) {
        JSON json = (JSON) JSON.toJSON(list);
        return json;
    }
}
